package com.itheima.controller;

import com.itheima.pojo.BlogSort;
import com.itheima.service.SortService;
import com.itheima.utils.PageResult;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/blogSort")
public class SortController {

    @Autowired
    private SortService sortService;

    //增加博客分类
    @PostMapping("/add")
    public Result add(@RequestBody BlogSort blogSort) {
        try {
            sortService.add(blogSort);
            return new Result(StatusCode.SUCCESS, "插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "插入失败");
        }
    }

    //编辑博客标签
    @PutMapping("/edit")
    public Result edit(@RequestBody BlogSort blogSort) {
        try {
            sortService.edit(blogSort);
            return new Result(StatusCode.SUCCESS, "编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "编辑失败");
        }
    }

    //批量删除标签
    @DeleteMapping("/deleteBatch")
    public Result delete(@RequestBody List<BlogSort> list) {
        try {
            sortService.deleteBatch(list);
            return new Result(StatusCode.SUCCESS, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "删除失败");
        }
    }

    //查询所有标签
    @GetMapping("/getList")
    public Result list(@RequestBody BlogSort blogSort) {
        try {
            if (blogSort.getCurrentPage() == null || blogSort.getPageSize() == null) {
                throw new RuntimeException("currentPage和pageSize都不能为空");
            }
            PageResult<BlogSort> pageResult = sortService.getList(blogSort);
            return new Result(StatusCode.SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "查询失败");
        }
    }

    //置顶标签
    @PostMapping("/stick")
    public Result stick(@RequestBody BlogSort blogSort) {
        try {
            sortService.stick(blogSort);
            return new Result(StatusCode.SUCCESS, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "操作失败");
        }
    }

    //引用量排序标签
    @PostMapping("/blogSortByCite")
    public Result blogSortSortByCite() {
        try {
            sortService.blogSortByCite();
            return new Result(StatusCode.SUCCESS, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "操作失败");
        }
    }

    //点击量排序标签
    @PostMapping("/blogSortByClickCount")
    public Result blogSortSortByClickCount() {
        try {
            sortService.blogSortByClickCount();
            return new Result(StatusCode.SUCCESS, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.FAIL, "操作失败");
        }
    }


}
